package de.hbrs.ooka.entities;

import de.hbrs.ooka.entities.dtos.PaperTransferObject;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "paperentity")
@NamedQueries({
    @NamedQuery(name = "findPaperByID", query = "SELECT p FROM PaperEntity p WHERE p.id = :id"),
    @NamedQuery(name = "findPaperByName", query = "SELECT p FROM PaperEntity p WHERE p.m_filename = :name"),
    @NamedQuery(name = "findAllPapers", query = "SELECT p FROM PaperEntity p")
   // , @NamedQuery(name = "findPaperForConference", query = "")
})
public class PaperEntity implements PaperTransferObject, Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String m_filename;
    private String m_title;
    private String m_content;
    private double m_rating;
    private int m_rateCount;
    private boolean m_accepted;
    private String m_textRating;
    @ManyToOne
    private ConferenceEntity m_submittedConference;
    @OneToOne
    private UserEntity m_author;
    @OneToMany
    private List<UserEntity> m_reviewerList = new ArrayList<UserEntity>();

    public PaperEntity() {
    }

    public PaperEntity(String filename, String title, String content) {
        this.m_filename = filename;
        this.m_title = title;
        this.m_content = content;
    }

    public void setContent(String content) {
        this.m_content = content;
    }

    @Override
    public String getContent() {
        return this.m_content;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String getFilename() {
        return this.m_filename;
    }

    public void setFilename(String filename) {
        this.m_filename = filename;
    }

    public void setAuthor(UserEntity author) {
        this.m_author = author;
    }

    @Override
    public UserEntity getAuthor() {
        return this.m_author;
    }

    @Override
    public List<UserEntity> getReviewer() {
        return m_reviewerList;
    }

    public void setReviewer(List<UserEntity> reviewer) {
        this.m_reviewerList.addAll(reviewer);
    }

    public void addReviewer(UserEntity reviewer) {
        if (! this.m_reviewerList.contains(reviewer)){
            this.m_reviewerList.add(reviewer);
        }
    }

    @Override
    public double getRating() {
        return m_rating;
    }

    public void setRating(double m_rating) {
        this.m_rating = m_rating;
    }

    @Override
    public boolean isAccepted() {
        return m_accepted;
    }

    public void setAccepted(boolean m_accepted) {
        this.m_accepted = m_accepted;
    }

    public void setSubmittedConference(ConferenceEntity conf) {
        this.m_submittedConference = conf;
    }
    
    @Override
    public ConferenceEntity getSubmittedConference() {
        return this.m_submittedConference;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (new Integer(id)).hashCode();
        hash += (this.m_filename != null ? this.m_filename.hashCode() : 0);
        hash += (this.m_title != null ? this.m_title.hashCode() : 0);
        hash += (this.m_content != null ? this.m_content.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof PaperEntity)) {
            return false;
        }
        PaperEntity other = (PaperEntity) object;
        return this.id == other.id;
    }

    @Override
    public String toString() {
        return "de.hbrs.ooka.beans.entities.PaperEntity[ id=" + id + " ]";
    }

    public void setTextualRating(String reasons) {
        this.m_textRating = reasons;
    }
}
